package com.wwwarehouse.financialcenter.fragment.moneywarehouse.billingrecords;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.button.StateButton;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.financialcenter.R;
import com.wwwarehouse.financialcenter.bean.bill.ChargingRecordBean;
import com.wwwarehouse.financialcenter.bean.bill.SerializableMap;
import com.wwwarehouse.financialcenter.constant.FinancialConstant;
import com.wwwarehouse.financialcenter.fragment.base.TheParentFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Joseph on 2017/11/15.
 *         <p/>
 *         实时计费记录子类列表界面
 */

public class ChargingRecordChildFragment extends TheParentFragment {

    ListView lvlist;
    List<ChargingRecordBean.ListBean> mList;
    InfomationListAdaper listAdaper ;
    int num = 7;    //每一页的个数
    int pos;        //当前页数
    int count;      //总共几页
    int mItemHeight; //每一个条目的高度
    String type ;
    private static final int GET_LIST_CHARGE_RECORD =1;
    private String businessId;
    @Override
    protected int getContentId() {
        return R.layout.financial_center_fragment_list;
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    @Override
    protected void init(View view) {
        lvlist = findView(view, R.id.lv_list);
    }

    @Override
    protected void loadDatas() {
        try {
            Bundle b = getArguments();
            SerializableMap serializableMap = (SerializableMap) b.getSerializable("map");
            Map map = serializableMap.getMap();
            businessId = (String) map.get("businessId");
            pos = b.getInt("pos");
            num = b.getInt("num");
            mList = new ArrayList<>();
            if (b != null) {
                int pos = b.getInt("pos");
                int num = b.getInt("num");

                map.put("page", pos);
                map.put("size", num);

                httpPost(FinancialConstant.URL_LIST_CHARGE_RECORD, map, GET_LIST_CHARGE_RECORD, true, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setTitle() {

    }

    private class InfomationListAdaper extends BaseAdapter {

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHodler viewHodler;
            if (convertView == null) {
                viewHodler = new ViewHodler();
                convertView = LayoutInflater.from(mActivity).inflate(R.layout.financial_center_adapter_charging_record, parent, false);
                ViewTreeObserver vto = lvlist.getViewTreeObserver();
                final View finalConvertView = convertView;
                vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        lvlist.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        ViewGroup.LayoutParams layoutParams = finalConvertView.getLayoutParams();
                        mItemHeight = lvlist.getHeight() / num;
                        layoutParams.height = mItemHeight;
                        LogUtils.showInfoLog("mItemHeight:"+mItemHeight);
                        finalConvertView.setLayoutParams(layoutParams);
                        LogUtils.showInfoLog("getHeight:"+lvlist.getHeight()+"mItemHeight:"+mItemHeight);
                    }
                });
                viewHodler.mName = (TextView) convertView.findViewById(R.id.goods_name);
                viewHodler.mModeButton = (StateButton) convertView.findViewById(R.id.mode_button);
                viewHodler.mBrand = (TextView) convertView.findViewById(R.id.brand);
                viewHodler.mMoney = (TextView) convertView.findViewById(R.id.tv_money);
                viewHodler.mTime = (TextView) convertView.findViewById(R.id.goods_time);
                viewHodler.mImg = (ImageView) convertView.findViewById(R.id.img);
                convertView.setTag(viewHodler);
            } else {
                viewHodler = (ViewHodler) convertView.getTag();
            }
            //int chargeType = mList.get(position).getChargeType();    //	费用类型（0=订购商品，1=取消订购，2=仓储契约，3=劳务）
            String whichSide = mList.get(position).getWhichSide();   //supply  卖  demand 买
           viewHodler.mName.setText(mList.get(position).getChargeTypeDesc());
            viewHodler.mTime.setText(mList.get(position).getChargeGeneratedTime());
            viewHodler.mImg.setImageResource(whichSide.equals("supply")?R.drawable.financial_center_sell:R.drawable.financial_center_buy_blue);
            viewHodler.mBrand.setText(mList.get(position).getOtherSideName());
            viewHodler.mMoney.setText(mList.get(position).getRecordAmount());
            String recordStatusDesc = mList.get(position).getRecordStatusDesc();
            viewHodler.mModeButton.setText(recordStatusDesc);
            String recordStatus = mList.get(position).getRecordStatus();
           if (TextUtils.equals(recordStatus,"2")){
               //已失效
               viewHodler.mImg.setImageResource(whichSide.equals("supply")?R.drawable.financial_center_sell_gray:R.drawable.financial_center_buy_gray);
                viewHodler.mName.setTextColor(getResources().getColor(R.color.common_color_c7_96999e));
                viewHodler.mMoney.setTextColor(getResources().getColor(R.color.common_color_c7_96999e));
                viewHodler.mModeButton.setTextColor(Color.parseColor("#FEFEFE"));
               viewHodler.mModeButton.setUnableBackgroundColor(getResources().getColor(R.color.common_color_c8_d5d7dc));

           }else if (TextUtils.equals(recordStatus,"1")){
               //已入账的标识不显示
               viewHodler.mModeButton.setVisibility(View.GONE);
           }
            switch (recordStatusDesc){


            }
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ChargingRecordDetailsFragment fragment = new ChargingRecordDetailsFragment();
                    Bundle  bundle = new Bundle();
                    bundle.putParcelable("data",mList.get(position));
                    bundle.putString("businessId",businessId);
                    fragment.setArguments(bundle);
                    pushFragment(fragment);
                }
            });
            return convertView;
        }
        @Override
        public int getCount() {
            return mList.size();
        }
        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
    }
    class ViewHodler {
        TextView mName;
        StateButton mModeButton;
        TextView mTime;
        TextView mBrand;
        TextView mMoney;
        ImageView mImg;
    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        switch (what){
            case GET_LIST_CHARGE_RECORD:
                if (TextUtils.equals("0", commonClass.getCode())) {
                    ChargingRecordBean recordBean = JSON.parseObject(commonClass.getData().toString(), ChargingRecordBean.class);
                     mList = recordBean.getList();

                    listAdaper = new InfomationListAdaper();
                    lvlist.setAdapter(listAdaper);
                }
                break;
        }

    }

    @Override
    public void onReLoad(int code) {

    }
}
